<?php
class BugController extends Zend_Controller_Action{
	
	public function submitAction(){	
		require "/../forms/BugReportForm.php";
		require "/../models/Bug.php";	
		$bugReportForm  = new Form_BugReportForm();
		$bugReportForm->setAction('/Form/public/Bug/submit');
		$bugReportForm->setMethod('post');
		if($this->getRequest()->isPost()){
			if($bugReportForm->isValid($_POST)){
				$bugModel = new Model_Bug();					
				$result = $bugModel->createBug(
					$bugReportForm->getValue('username'),
					$bugReportForm->getValue('password'),
					$bugReportForm->getValue('email'));	
				if($result)
				{
					$this->_forward('confirm');
				}
			}
		}
		$this->view->form = $bugReportForm;
	}
	public function confirmAction(){
		
	}
	public function listAction(){
		require "/../models/Bug.php";
		$bugModel = new Model_Bug();
		$this->view->bugs = $bugModel->fetchBugs();
	}
	
	public function deleteAction(){
		require "/../models/Bug.php";		
		$bugModel = new Model_Bug();
		$id = $this->_request->getParam('id');		
		$bugModel->deleteBug($id);
		//return  $this->_forward('list');
	}
	public function editAction() {
		require "/../models/Bug.php";
		require "/../forms/BugReportForm.php";
		$bugModel = new Model_Bug();
		$bugReportForm = new Form_BugReportForm();
		$bugReportForm->setAction('edit');
		$bugReportForm->setMethod('post');
		if($this->_request->isPost()){
			if($bugReportForm->isValid($_POST)){
				$result = $bugModel->updateBug(
					$bugReportForm->getValue('id'),
					$bugReportForm->getValue('username'),
					$bugReportForm->getValue('password'),
					$bugReportForm->getValue('email'));				
			}
			return $this->_forward('confirm');
		}
		else{
			$id = $this->_request->getParam('id');
			$bug = $bugModel->find($id)->current();
			$bugReportForm->populate($bug->toArray());
		}
		$id = $this->_request->getParam('id');
		$bug = $bugModel->find(trim($id))->current();
		$bugReportForm->populate($bug->toArray()); 
		$this->view->bug = $bug;
		$this->view->form = $bugReportForm;
	}
}